################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2024)

# Description:

## Code to make Figure A5: "Within-party divergence over time, controlling for committee but not party"

################################################################################

# Packages 

library(data.table)
library(dplyr)
library(showtext)
library(ggplot2)

# Directories (the working directory is ./scripts, set by master.R)

data.dir <-  "../data/3_model_output"
fig.dir  <-  "../results/figures"

# Add montserrat font

font_add_google(name = "Montserrat", family = "Montserrat")
showtext_auto()

# Plot function

p_function <- function(data, title){
  
  ggplot(data=data, aes(y=pi, x=sessions, group=ind, fill=ind)) +
   
    # Elections
    geom_vline(xintercept = vertical.lines, colour="gray80", linewidth = 0.2) +  
    
   # Estimated divergence
    geom_line() +
    # Placebo estimates
    geom_line(data=data, aes(sessions, mean), linetype = "dashed") +
    geom_ribbon(aes(ymin=min_pi, ymax=max_pi), fill="darkgray", alpha=0.45, linetype=0) +
    geom_point(size=4) +
    

    
    # Axis
    scale_x_continuous(breaks = seq(from=1981, to=2021, by=4)) +
    scale_y_continuous(limits = c(0.5, 0.506)) +
    # Labs
    labs(title=title,
         x ="Session", y = "Average divergence") +
    # Theme
    theme(
      panel.grid = element_blank(),
      legend.position = "none",
      legend.title = element_blank(),
      # panel.grid.major.x= element_line(linewidth = 0.2, colour = "grey90"),
      panel.background = element_rect(fill = "transparent"),
      plot.background = element_rect(fill = "transparent"),
      axis.ticks.length = unit(0, "lines"),
      axis.text.x = element_text(family = "Montserrat", size = 16, angle = 45, hjust = 1),
      axis.text.y = element_text(family = "Montserrat", size = 16),
      axis.title.y = element_text(family = "Montserrat", size = 16),
      axis.title.x = element_text(family = "Montserrat", size = 16)
    )
  
}

## Data

### Common macros

sessions <- c(1982:2021)
vertical.lines <- c(1981.5, 1985.5, 1989.5, 1993.5, 1997.5,2001.5, 2005.5, 2009.5, 2013.5, 2017.5, 2021.5)

data.vector = c("Women/Men" = "gender_com", 
                "Old/Young" = "age_com",
                "Urban/Rural" = "town_com",
                "White-collar/Blue-collar" = "occ_com")

data.function <- function(pi_data = c("Women/Men" = "gender_comparty")){
  
  totpi_sort <- fread(file = paste(data.dir, paste0(pi_data, "_placebo", ".csv"), 
                                   sep = "/"), drop = "sessions")
  
  max_pi <- totpi_sort[,5]
  min_pi <- totpi_sort[,95]
  max_pi_90 <- totpi_sort[,10]
  min_pi_90 <- totpi_sort[,90]
  
  estimated_pi <- fread(paste(data.dir, paste0(pi_data, ".csv"), 
                              sep = "/"), header=T)
  
  bounds <- as.data.frame(cbind(sessions, max_pi, min_pi)) 
  bounds_90 <- as.data.frame(cbind(sessions, max_pi_90, min_pi_90))
  bounds$ind <- rep(95, len=nrow(bounds))
  bounds_90$ind <- rep(90, len=nrow(bounds_90))
  
  colnames(bounds)[2] <- "max_pi"
  colnames(bounds)[3] <- "min_pi"
  colnames(bounds_90)[2] <- "max_pi"
  colnames(bounds_90)[3] <- "min_pi"
  
  bounds <- as.data.frame(rbind(bounds, bounds_90))
  
  bounds$mean <-  rowMeans(totpi_sort)
  estimated_pi <- rbind(estimated_pi, estimated_pi)
  bounds$pi <- estimated_pi$pi
  
  return(bounds)
  # data.list[[names(pi_data)]] <- bounds
  
}

d <- lapply(data.vector, data.function)

### Plotting


#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#A Gender----
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p_gender <- p_function(data = d[["Women/Men"]], title = "Women/Men")


ggsave(plot = p_gender, 
       file = paste(fig.dir, "figA5a.pdf", sep = "/"), 
       device = cairo_pdf)

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#B Age----
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p_age <- p_function(data = d[["Old/Young"]], title = "Old/Young")


ggsave(plot = p_age, 
       file = paste(fig.dir, "figA5b.pdf", sep = "/"), device = cairo_pdf)

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#C Urbanicity----
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


p_urban <- p_function(data = d[["Urban/Rural"]], title = "Urban/Rural")


ggsave(plot = p_urban, 
       file = paste(fig.dir, "figA5c.pdf", sep = "/"), 
       device = cairo_pdf)


#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#D Social background----
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p_class <- p_function(data = d[["White-collar/Blue-collar"]], 
                      title = "White-collar/Blue-collar")


ggsave(plot = p_class, 
       file = paste(fig.dir, "figA5d.pdf", sep = "/"), 
       device = cairo_pdf)

